package com.zhss.eshop.schedule.service.Impl;

import com.zhss.eshop.common.util.DateProvider;
import com.zhss.eshop.common.util.ObjectUtils;
import com.zhss.eshop.logistics.service.LogisticsService;
import com.zhss.eshop.order.domain.dto.OrderInfoDTO;
import com.zhss.eshop.order.domain.dto.OrderItemDTO;
import com.zhss.eshop.schedule.domain.SaleDeliveryScheduleResult;
import com.zhss.eshop.schedule.service.SaleDeliveryOrderBuilder;
import com.zhss.eshop.schedule.service.SaleDeliveryScheduler;
import com.zhss.eshop.wms.constant.SaleDeliveryOrderStatus;
import com.zhss.eshop.wms.domain.dto.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 默认的销售出库单构造器
 * @author suyuan
 *
 */
@Component
@Scope("prototype")
public class DefaultSaleDeliveryOrderBuilder implements SaleDeliveryOrderBuilder {

	/**
	 * 要构造的销售出库单
	 */
	private WmsSaleDeliveryOrderDTO wmsSaleDeliveryOrderDTO = new WmsSaleDeliveryOrderDTO();

	/**
	 * 销售出库调度器
	 */
	@Autowired
	private SaleDeliveryScheduler saleDeliveryScheduler;
	/**
	 * 日期辅助组件
	 */
	@Autowired
	private DateProvider dateProvider;
	/**
	 * 物流中心接口
	 */
	@Autowired
	private LogisticsService logisticsService;

	/**
	 * 设置订单相关的数据
	 * @param order 订单
	 * @return 销售出库单builder
	 */
	@Override
	public SaleDeliveryOrderBuilder setOrderRelatedData(
			OrderInfoDTO order) throws Exception {
		wmsSaleDeliveryOrderDTO.setOrderId(order.getId());
		order.clone(wmsSaleDeliveryOrderDTO);
		return this;
 	}

	/**
	 * 创建销售出库单条目
	 * @param orderItems 订单条目
	 * @return 销售出库单builder
	 */
	@Override
	public SaleDeliveryOrderBuilder createSaleDeliveryOrderItems(
			List<OrderItemDTO> orderItems) throws Exception {
		List<WmsSaleDeliveryOrderItemDTO> saleDeliveryOrderItems =
				new ArrayList<WmsSaleDeliveryOrderItemDTO>();

		for(OrderItemDTO orderItem : orderItems) {
			SaleDeliveryScheduleResult scheduleResult = saleDeliveryScheduler
					.getScheduleResult(orderItem);

			WmsSaleDeliveryOrderItemDTO saleDeliveryOrderItem =
					orderItem.clone(WmsSaleDeliveryOrderItemDTO.class);

			saleDeliveryOrderItem.setPickingItems(ObjectUtils.convertList(
					scheduleResult.getPickingItems(), WmsSaleDeliveryOrderPickingItemDTO.class));
			saleDeliveryOrderItem.setSendOutItems(ObjectUtils.convertList(
					scheduleResult.getSendOutDetails(), WmsSaleDeliveryOrderSendOutDetailDTO.class));

			saleDeliveryOrderItem.setGmtCreate(dateProvider.getCurrentTime());
			saleDeliveryOrderItem.setGmtModified(dateProvider.getCurrentTime());

			saleDeliveryOrderItems.add(saleDeliveryOrderItem);
		}

		wmsSaleDeliveryOrderDTO.setSaleDeliveryOrderItems(saleDeliveryOrderItems);

		return this;
	}

	/**
	 * 创建发货单
	 * @param order 订单
	 * @return 销售出库单builder
	 */
	@Override
	public SaleDeliveryOrderBuilder createSendOutOrder(
			OrderInfoDTO order) throws Exception {
		WmsSendOutOrderDTO sendOutOrder = new WmsSendOutOrderDTO();
		sendOutOrder.setSendOutOrderItems(new ArrayList<WmsSendOutOrderItemDTO>());

		//订单克隆到发货单里
		order.clone(sendOutOrder);
		sendOutOrder.setOrderId(order.getId());
		sendOutOrder.setGmtCreate(dateProvider.getCurrentTime());
		sendOutOrder.setGmtModified(dateProvider.getCurrentTime());

		for(OrderItemDTO orderItem : order.getOrderItems()) {
			//订单条目克隆到发货单条目
			WmsSendOutOrderItemDTO sendOutOrderItem = orderItem.clone(WmsSendOutOrderItemDTO.class);
			sendOutOrderItem.setGmtCreate(dateProvider.getCurrentTime());
			sendOutOrderItem.setGmtModified(dateProvider.getCurrentTime());
			sendOutOrder.getSendOutOrderItems().add(sendOutOrderItem);
		}

		wmsSaleDeliveryOrderDTO.setSendOutOrder(sendOutOrder);

		return this;
	}

	/**
	 * 创建物流单
	 * @param order 订单
	 * @return 销售出库单builder
	 */
	@Override
	public SaleDeliveryOrderBuilder createLogisticOrder(
			OrderInfoDTO order) throws Exception {
		WmsLogisticOrderDTO logisticOrder = logisticsService.applyLogisticOrder(order);
		wmsSaleDeliveryOrderDTO.setLogisticOrder(logisticOrder);
		return this;
	}

	/**
	 * 初始化销售出库单的状态
	 * @return 销售出库单builder
	 */
	@Override
	public SaleDeliveryOrderBuilder initStatus() throws Exception {
		wmsSaleDeliveryOrderDTO.setStatus(SaleDeliveryOrderStatus.EDITING);
		return this;
	}

	/**
	 * 初始化时间相关的字段
	 * @return 销售出库单builder
	 */
	@Override
	public SaleDeliveryOrderBuilder initTimes() throws Exception {
		wmsSaleDeliveryOrderDTO.setGmtCreate(dateProvider.getCurrentTime());
		wmsSaleDeliveryOrderDTO.setGmtModified(dateProvider.getCurrentTime());
		return this;
	}

	/**
	 * 创建最终构造好的销售出库单
	 * @return 销售出库单
	 */
	@Override
	public WmsSaleDeliveryOrderDTO create() throws Exception {
		return wmsSaleDeliveryOrderDTO;
	}

}
